package com.ruoyi.web.controller.wechat;

import com.ruoyi.common.exception.user.UserInvalidException;
import com.ruoyi.web.controller.wechat.response.FromType;
import com.ruoyi.web.core.config.EncryptUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2023-01-13 11:53:07
 */
@Slf4j
@Data
public class AuthController {

    @Autowired
    protected HttpServletRequest request;

    private static final String HEADER_ID = "ygid";

    private static final String FROM_ID = "from";

    protected String getOpenId() {
        String hfid = request.getHeader(HEADER_ID);
        if (StringUtils.isBlank(hfid)) {
            log.error("getOpenId is null[{}]");
            throw new UserInvalidException();
        }
        String decrypt = EncryptUtil.decrypt(hfid);
        if (StringUtils.isBlank(decrypt)) {
            log.error("getOpenId is null[{}]");
            throw new UserInvalidException();
        }
        return decrypt;
    }

    protected FromType getFrom() {
        String from = request.getHeader(FROM_ID);

        return FromType.query(from);
    }

    public static void main(String[] args) {
        String decrypt = EncryptUtil.encrypt("oHIZe5aYKAaNQfBFTi1p-Fy5BjV8");
        System.out.println(decrypt);
        System.out.println("https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132".length());
    }
}
